package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.GmallService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
public class GmallController {
    @Autowired
    GmallService gmallService;
    //    1：按照省份展示总成交额指标
    @RequestMapping("/sumPro")
    public Object sumPro(@RequestParam("nowDate") String  nowDate){
        List<Map> list = gmallService.sumPro(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray mapArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("bname"));
            mapObject.put("value",map.get("pmoney"));
            mapArray.add(mapObject);
        }

        dataObject.put("mapData",mapArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };

    @RequestMapping("/sumBrand")
    public Object sumBrand(@RequestParam("nowDate") String  nowDate){
        List<Map> list = gmallService.sumBrand(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray catArray = new JSONArray();
        for (Map map : list) {
            catArray.add(map.get("tname"));
        }
        dataObject.put("categories",catArray);
        JSONArray serArray = new JSONArray();
        JSONObject serObject = new JSONObject();
        serObject.put("name","品牌销售");


        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            dataArray.add(map.get("omoney"));
        }
        serObject.put("data",dataArray);
        serArray.add(serObject);
        dataObject.put("series",serArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };

     @RequestMapping("/isNew")
    public Object isNew(@RequestParam("nowDate") String  nowDate){
        List<Map> list = gmallService.isNew(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONObject dataObject = new JSONObject();
        JSONArray colArray = new JSONArray();

        JSONObject colObject5 = new JSONObject();
        colObject5.put("name","新老用户标志");
        colObject5.put("id","subsys1");
        colArray.add(colObject5);


        JSONObject colObject = new JSONObject();
        colObject.put("name","用户数");
        colObject.put("id","subsys");
        colArray.add(colObject);

        JSONObject colObject1 = new JSONObject();
        colObject1.put("name","总访问页面");
        colObject1.put("id","module");
        colArray.add(colObject1);

        JSONObject colObject2 = new JSONObject();
        colObject2.put("name","跳出率");
        colObject2.put("id","business");
        colArray.add(colObject2);

        JSONObject colObject3 = new JSONObject();
        colObject3.put("name","平均在线时长");
        colObject3.put("id","name");
        colArray.add(colObject3);

        JSONObject colObject4 = new JSONObject();
        colObject4.put("name","平均访问人数");
        colObject4.put("id","value");
        colArray.add(colObject4);

        dataObject.put("columns",colArray);


        JSONArray rowArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowObject = new JSONObject();
            rowObject.put("subsys1",map.get("newLogo"));
            rowObject.put("subsys",map.get("USER"));
            rowObject.put("module",map.get("sumPage"));
            rowObject.put("business",map.get("skip"));
            rowObject.put("name",map.get("avgtime"));
            rowObject.put("value",map.get("avguser"));
            rowArray.add(rowObject);
        }



        dataObject.put("rows",rowArray);
        jsonObject.put("data",dataObject);
        return jsonObject;
    };

    @RequestMapping("/sumCategory")
    public Object sumCategory(@RequestParam("nowDate") String  nowDate){
        List<Map> list = gmallService.sumCategory(nowDate);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",0);
        jsonObject.put("msg","");
        JSONArray dataArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapObject = new JSONObject();
            mapObject.put("name",map.get("cname"));
            mapObject.put("value",map.get("zb"));
            dataArray.add(mapObject);
        }


        jsonObject.put("data",dataArray);
        return jsonObject;
    };

}
